package com.gykj.admin.service.impl;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;

import com.gykj.admin.common.entity.UserRole;
import com.gykj.admin.service.UserRoleService;

/**
 * Created by rohto.li on 2017/11/03.
 */
@Service("userRoleService")
@Cacheable(value = "redis", keyGenerator = "cacheKeyGenerator")
public class UserRoleServiceImpl extends BaseService<UserRole> implements UserRoleService {


    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    public void addUserRole(UserRole userRole) {
        //删除
        Example example = new Example(UserRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userRole.getUserId());
        mapper.deleteByExample(example);
        //添加
        String[] roleids = userRole.getRoleIdArr();
        for (String roleId : roleids) {
            UserRole u = new UserRole();
            u.setUserId(userRole.getUserId());
            u.setRoleId(Integer.parseInt(roleId));
            mapper.insert(u);
        }

    }
}
